package StringOperation;

import com.sun.media.sound.RIFFInvalidDataException;

public class LongestPalindrome {

    //  CKG TODO 2023/4/19: 截取字符串里面的最长回文字串

    public String longestPalindrome(String s) {
     String res="";
        int length = s.length();
        for (int i = 0; i < length-1; i++) {
            String palindrome = palindrome(s, i, i);
            String palindrome1 = palindrome(s, i, i + 1);
            res=palindrome.length()>res.length()?palindrome:res;
            res=palindrome1.length()>res.length()?palindrome1:res;
        }
        return res;
    }

    public String palindrome(String s,int left,int  right ) {
        while (left>=0&&right<s.length()&&s.charAt(left)==s.charAt(right)){
            left--;
            right++;
        }
        if (left+1>right-1){
            return "";
        }
        return s.substring(left+1,right);
    }


//  CKG TODO 2023/4/16: 判断一个字符串是不是回文串
    public   boolean palindrome(String s){
        int left=0,right=s.length()-1;
        while (left< right){
            if (s.charAt(left)==s.charAt(right)){
                left++;
                right--;
            }else {
                return false;
            }
        }
        return  true;
    }

//  CKG TODO 2023/4/19:  判断回文单链表   左侧指针
    ListNode left;
    boolean isPalindrome(ListNode head) {
        left = head;
        return traverse(head);
    }
    boolean traverse(ListNode right) {
        if (right == null) return true;
        boolean res = traverse(right.next);
        // 后序遍历代码
        res = res && (right.val == left.val);
        left = left.next;
        return res;
    }











    public static void main(String[] args) {

        LongestPalindrome longestPalindrome = new LongestPalindrome();
        System.out.println(longestPalindrome.longestPalindrome("sbcbdfg"));
//        System.out.println(longestPalindrome.palindrome("aba"));
//        String a="abc";
//        System.out.println(a.substring(1,0));


    }

}
